{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Look and Say Sequence\n",
    "\n",
    "[The Original Question](https://mp.weixin.qq.com/s/loqKOuQakuaxzHZcRb6XXg)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "A look-and-say sequence is defined as the integer sequence beginning with a single digit in which the next term is obtained by describing the previous term. An example is easier to understand:\n",
    "\n",
    "```text\n",
    "1       # Initial sequence\n",
    "11      # A 1\n",
    "21      # Two 1\n",
    "1211    # A 2 and A 1\n",
    "111221  # A 1, a 2 and two 1\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def look_and_say(index):\n",
    "    sequence = '1'\n",
    "    for i in range(index):\n",
    "        splits = re.findall(r'(\\d)(\\1*)', sequence)\n",
    "        sequence = ''.join([str(len(k) + len(v)) + k[0] for (k, v) in splits])\n",
    "    return sequence"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11131221133112132113212221\n"
     ]
    }
   ],
   "source": [
    "print(look_and_say(10))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
